var ScoreEffectItemController = require('geoScoreEffectItemController');
/**
 * 动画效果组，比如界面上的一排五星，五星可以有各种动画表示得分效果
 */
cc.Class({
    extends: cc.Component,

    properties: {
        scoreEffectPrefab: {
            default: null,
            type: cc.Prefab,
            tooltip: '构成计分效果的预制，内含geoScoreEffectItemController',
        },
        scoreEffectContainer: {
            default: null,
            type: cc.Node,
            tooltip: '用于承载计分效果的节点',
        },

        score: {
            default: 0,
            type: cc.Integer,
            tooltip: '当前内部计分值'
        },

        scoreEffectItems: {
            default: [],
            type: [ScoreEffectItemController],
            tooltip: '用于显示计分效果的道具'
        }

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (this.scoreEffectPrefab != null) {
            this.scoreEffectContainer = this.scoreEffectContainer || this.node;
            // 从预制创建计分效果
            var scoreEffect = cc.instantiate(this.scoreEffectPrefab);
            this.scoreEffectContainer.addChild(scoreEffect);
            //
            scoreEffect.getComponentsInChildren(ScoreEffectItemController).forEach(e => {
                this.scoreEffectItems.push(e);
            }, this);
        }
    },

    /**
    * 预先设置分数，没有动画效果
    * @param {int} score 
    */
    presetScore(score) {
        this.score = (score < 0) ? 0 : score;
        // 先清空所有得分状态
        this.scoreEffectItems.forEach(item => {
            item.presetState(false);
        });
        // 设置响应得分状态
        for (var i = 0; i < this.score && i < this.scoreEffectItems.length; ++i) {
            var item = this.scoreEffectItems[i];
            item.presetState(true);
        }
    },

    /**
     * 根据传入的分数，显示对应的得分效果
     * @param {number} score 设置要显示的分数
     * @param {cc.callFunc} complete 分数设置效果完毕后回调
     * @param {cc.callFunc} step 设置每一分时的回调，可以忽略
     * @example
     * // 设置分值为3
     * this.setScore(3);
     * // 设置分值为3，且有回调函数
     * this.setScore(3, cc.callFunc(()=>{
     *      cc.log("complete....")
     * }));
     */
    setScore(score, complete, step) {
        // 为顺序调用action做准备
        var fake = cc.callFunc(() => { });
        var actions = [fake, fake];
        //
        if (score <= 0) {
            // 1, 如果传入0，就清空所有得分效果
            this.score = 0;
            this.scoreEffectItems.forEach(e => {
                e.setState(false);
            });
        } else {
            // 2, 如果传入不是0，就播放从当前分数到传入分数的得分效果
            for (var i = this.score; i < score && i < this.scoreEffectItems.length; ++i) {
                var item = this.scoreEffectItems[i];
                actions.push(cc.callFunc((caller, e) => {
                    e.setState(true)
                },
                    this, item));
                actions.push(cc.delayTime(1));
                if (step)
                    actions.push(cc.callFunc(step));
            }
        }
        // 设置内部得分
        this.score = score;
        // 加入结束回调
        if (complete)
            actions.push(cc.callFunc(complete));
        if (actions.length > 0)
            this.node.runAction(cc.sequence(actions));
    },
    /**
     * 丢分效果
     * @param {[int]} scores 丢分的位置
     * @param {function} complete 
     */
    loseScore(scores, complete, step) {
        // 为顺序调用action做准备
        var fake = cc.callFunc(() => { });
        var actions = [fake, fake];

        for (var n = 0; n < scores.length; ++n) {
            var i = scores[n];
            var item = this.scoreEffectItems[i-1];
            actions.push(cc.callFunc((caller, e) => {
                e.setState(false)
            },
                this, item));
            actions.push(cc.delayTime(1));
            if (step)
                actions.push(cc.callFunc(step));
        }
        // 加入结束回调
        if (complete)
            actions.push(cc.callFunc(complete));
        if (actions.length > 0)
            this.node.runAction(cc.sequence(actions));
    },

    // update (dt) {},
});
